﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace Coding4Fun.Maelstrom.Tablet
{
	public sealed partial class Blade : UserControl
	{
		private double _rotX;
		private double _rotY;
		private double _rotZ;

		public Blade()
		{
			this.InitializeComponent();
		}

		public double Speed { get; set; }

		public double X
		{
			set { this.SetValue(Canvas.LeftProperty, value); }
			get { return (double)this.GetValue(Canvas.LeftProperty); }
		}

		public double Y
		{
			set { this.SetValue(Canvas.TopProperty, value); }
			get { return (double)this.GetValue(Canvas.TopProperty); }
		}


		public void SetBlade(int blade)
		{
			Image b = this.FindName("img" + blade) as Image;
			b.Visibility = Visibility.Visible;
		}

		public void SetRotation(double _x, double _y, double _z)
		{
			_rotX = _x;
			_rotY = _y;
			_rotZ = _z;
			projection.RotationX = _rotX;
			projection.RotationY = _rotY;
			projection.RotationZ = _rotZ;
		}

		public void SetScale(double _x, double _y)
		{
			scale.ScaleX = _x;
			scale.ScaleY = _y;
		}

		public void Update()
		{
			_rotX += Speed;
			_rotZ += Speed;
			_rotY += Speed;
            if (_rotX > 180) _rotX -= 360.0;
            if (_rotX < -180) _rotX += 360.0;
            if (_rotY > 180) _rotY -= 360.0;
            if (_rotY < -180) _rotY += 360.0;
            if (_rotZ > 180) _rotZ -= 360.0;
            if (_rotZ < -180) _rotZ += 360.0;

			projection.RotationX = _rotX;
			projection.RotationY = _rotY;
			projection.RotationZ = _rotZ;
		}

		public void UpdateExit()
		{
			_rotX *= .98;
			_rotZ *= .98;
			_rotY += (90.0 - _rotY) * .1;
			projection.RotationX = _rotX;
			projection.RotationY = _rotY;
			projection.RotationZ = _rotZ;
		}
	}
}
